package cn.gemiman.daoyi.module.cms.service.blogtalk;

import cn.gemiman.daoyi.framework.common.enums.UserTypeEnum;
import cn.gemiman.daoyi.framework.common.pojo.PageResult;
import cn.gemiman.daoyi.framework.common.util.object.BeanUtils;
import cn.gemiman.daoyi.framework.common.util.servlet.ServletUtils;
import cn.gemiman.daoyi.framework.ip.core.Area;
import cn.gemiman.daoyi.framework.ip.core.utils.IPUtils;
import cn.gemiman.daoyi.module.cms.controller.admin.blogtalk.vo.BlogTalkPageReqVO;
import cn.gemiman.daoyi.module.cms.controller.admin.blogtalk.vo.BlogTalkSaveReqVO;
import cn.gemiman.daoyi.module.cms.dal.dataobject.blogtalk.BlogTalkDO;
import cn.gemiman.daoyi.module.cms.dal.mysql.blogtalk.BlogTalkMapper;
import cn.gemiman.daoyi.module.member.api.user.MemberUserApi;
import cn.gemiman.daoyi.module.member.api.user.dto.MemberUserRespDTO;
import cn.gemiman.daoyi.module.system.api.user.AdminUserApi;
import cn.gemiman.daoyi.module.system.api.user.dto.AdminUserRespDTO;
import cn.hutool.core.text.CharSequenceUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import static cn.gemiman.daoyi.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.gemiman.daoyi.module.cms.enums.ErrorCodeConstants.BLOG_TALK_NOT_EXISTS;

/**
 * 说说 Service 实现类
 *
 * @author 道一源码
 */
@Service
@Validated
public class BlogTalkServiceImpl implements BlogTalkService {

    @Resource
    private BlogTalkMapper blogTalkMapper;
    @Resource
    private AdminUserApi adminUserApi;
    @Resource
    private MemberUserApi memberUserApi;

    @Override
    public Long createBlogTalk(BlogTalkSaveReqVO createReqVO) {
        String clientIP = ServletUtils.getClientIP();
        createReqVO.setCreatorIp(clientIP);
        if (CharSequenceUtil.isNotBlank(clientIP)) {
            Area area = IPUtils.getArea(clientIP);
            if (null != area) {
                createReqVO.setCreatorCity(area.getName());
            }
        }
        // 插入
        BlogTalkDO blogTalk = BeanUtils.toBean(createReqVO, BlogTalkDO.class);
        if (UserTypeEnum.ADMIN.getValue().equals(blogTalk.getUserType())) {
            AdminUserRespDTO checkedData = adminUserApi.getUser(blogTalk.getUserId()).getCheckedData();
            blogTalk.setUserNickname(checkedData.getNickname());
            blogTalk.setUserAvatar(checkedData.getAvatar());
        }
        if (UserTypeEnum.MEMBER.getValue().equals(blogTalk.getUserType())) {
            MemberUserRespDTO checkedData = memberUserApi.getUser(blogTalk.getUserId()).getCheckedData();
            blogTalk.setUserNickname(checkedData.getNickname());
            blogTalk.setUserAvatar(checkedData.getAvatar());
        }
        blogTalkMapper.insert(blogTalk);
        // 返回
        return blogTalk.getId();
    }

    @Override
    public void updateBlogTalk(BlogTalkSaveReqVO updateReqVO) {
        // 校验存在
        validateBlogTalkExists(updateReqVO.getId());
        // 更新
        BlogTalkDO updateObj = BeanUtils.toBean(updateReqVO, BlogTalkDO.class);
        blogTalkMapper.updateById(updateObj);
    }

    @Override
    public void deleteBlogTalk(Long id) {
        // 校验存在
        validateBlogTalkExists(id);
        // 删除
        blogTalkMapper.deleteById(id);
    }

    private void validateBlogTalkExists(Long id) {
        if (blogTalkMapper.selectById(id) == null) {
            throw exception(BLOG_TALK_NOT_EXISTS);
        }
    }

    @Override
    public BlogTalkDO getBlogTalk(Long id) {
        return blogTalkMapper.selectById(id);
    }

    @Override
    public PageResult<BlogTalkDO> getBlogTalkPage(BlogTalkPageReqVO pageReqVO) {
        return blogTalkMapper.selectPage(pageReqVO);
    }

}
